package com.qf.sale.service.impl;

import com.qf.lw.mapper.OrderMapper;
import com.qf.lw.pojo.CustomerPojo;
import com.qf.lw.pojo.GoodsPojo;
import com.qf.lw.pojo.OrderDetailPojo;
import com.qf.lw.pojo.OrderPojo;
import com.qf.lw.util.StringUUid;
import com.qf.sale.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    OrderMapper orderMapper;
    @Override
    public OrderPojo create(int aid, String[] gids, Map<String, GoodsPojo> shopCar,int addressId) {
        //构建订单信息

        OrderPojo orderPojo=new OrderPojo();
        orderPojo.setAid(aid);//用户名id
        orderPojo.setAddressId(addressId);//地址
        orderPojo.setOid(StringUUid.uuid());//订单信息的主键
        //缺少一个当前商品的总价格
        float total=getTotalPrice(gids,shopCar);
        orderPojo.setOtotal(total);
        //保存订单信息
        boolean flg=orderMapper.createOrder(orderPojo);
        //如果订单创建成功，给这个订单创建订单详情
        if(flg){
            //构建一个订单详情的列表
            List<OrderDetailPojo> details=createOrderDetails(gids,shopCar,orderPojo.getOid());
            //保存订单详情
            boolean bl= orderMapper.createOrderDetails(details);
            if(bl){
                //需要将订单详情添加到OrderPojo中
                orderPojo.setDetails(details);
                //把结算成功的商品从购物车移除
                for(String gid:gids){
                    shopCar.remove(gid);
                }
                //返回保存订单和详情
                return orderPojo;
            }

        }
        return null;
    }

    @Override
    public boolean cancelOrder(String oid) {
        return orderMapper.cancelOrder(oid);
    }

    @Override
    public boolean apaySuccess(String oid) {
        return orderMapper.apaySuccess(oid);
    }

    @Override
    public CustomerPojo ordershow(int aid) {

        return orderMapper.ordershow(aid);
    }

    /**
     * 构建订单详情的列表
     * @param gids 参与结算的商品id
     * @param shopCar 购物车
     * @param oid 当前订单id
     * @return 订单详情的list
     */
    private List<OrderDetailPojo> createOrderDetails(String[] gids, Map<String,GoodsPojo> shopCar, String oid) {
        List<OrderDetailPojo> list=new ArrayList<OrderDetailPojo>();
        for(String gid:gids){
          shopCar.get(gid);//获得购物车一条订单项详情
          OrderDetailPojo  odp=new OrderDetailPojo();//创建购物车里的一条订单详情
            odp.setOdid(StringUUid.uuid());//订单详情id
            odp.setOid(oid);//当前订单的id
            odp.setGoodsPojo(shopCar.get(gid));//当前商品的详情
            odp.setOdnumber(shopCar.get(gid).getNumber());//当前商品的数量
            odp.setOdprice(shopCar.get(gid).getGprice()*shopCar.get(gid).getGdiscount());//当前商品的总价
            list.add(odp);//将当前订单详情添加到集合里面
        }
        return list;
    }
    /**
     * 计算当前所有参与结算的商品的总价格
     * @param gids
     * @param shopCar
     * @return
     */
    public float getTotalPrice(String[] gids,Map<String ,GoodsPojo> shopCar){
        float total=0.0f;
        for(String gid:gids){
            shopCar.get(gid);//商品信息
            total+=shopCar.get(gid).getGprice()*shopCar.get(gid).getGdiscount()*shopCar.get(gid).getNumber();
        }
        return total;
    }
}
